import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/login'
import layout from '../views/layout'
import { getToken } from '@/utils/token'
// 引入layout页的一些组件
import chart from '@/views/layout/chart'
import user from '@/views/layout/user'
import enterprise from '@/views/layout/enterprise'
import question from '@/views/layout/question'
import subject from '@/views/layout/subject'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/layout',
    component: layout,
    children: [
      { path: 'chart', component: chart },
      { path: 'user', component: user },
      { path: 'enterprise', component: enterprise },
      { path: 'question', component: question },
      { path: 'subject', component: subject }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  // console.log('from__:', from)
  // const token = getToken()
  // console.log(token)
  // next()

  if (to.path == '/login') {
    next()
  } else {
    const token = getToken()
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
